package com.kungfuhacking.wristbandpro.find.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.find.bean.ConfirmHistoryEnty;
import com.kungfuhacking.wristbandpro.find.presenter.IIDConfirmePresenter;
import com.kungfuhacking.wristbandpro.find.view.IDConfirmeView;
import com.kungfuhacking.wristbandpro.location.presenter.ConfirmAdapter;
import com.kungfuhacking.wristbandpro.widget.swipe.DividerItemDecoration;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 实名认证
 * Created by Aaron on 17/6/5.
 */

public class IDConfirmeActivity extends BaseActivity implements View.OnClickListener, IDConfirmeView {

    private static final int REQUEST_CODE = 0x633;
    private EditText etNum;
    private List<ConfirmHistoryEnty> entry = new ArrayList<>();
    private IIDConfirmePresenter iidConfirmePresenter = new IIDConfirmePresenter(this);
    private LinearLayout llInput;
    private RecyclerView recycleView;
    private ConfirmAdapter confirmAdapter;

//    private WebView wv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_confirm);

        initWidget();
        //获取认证列表
        iidConfirmePresenter.getIdentityList();

    }

    private void initWidget() {
//        wv = (WebView) findViewById(R.id.wv);
        TitleBarView tbv = (TitleBarView) findViewById(R.id.tbv);
        TextView tvScan = (TextView) findViewById(R.id.tvScan);
        TextView tvInput = (TextView) findViewById(R.id.tvInput);
        llInput = (LinearLayout) findViewById(R.id.llInput);

        recycleView = (RecyclerView) findViewById(R.id.recycleView);

        tvScan.setOnClickListener(this);
        tvInput.setOnClickListener(this);
        tbv.setTitle(getResources().getString(R.string.id_confirme));
        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        etNum = (EditText) findViewById(R.id.etNum);

        TextView tvSub = (TextView) findViewById(R.id.tvSub);
        tvSub.setOnClickListener(this);
        initRecycleView();

    }

    private void initRecycleView() {

        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setItemAnimator(new DefaultItemAnimator());
        confirmAdapter = new ConfirmAdapter(this, entry);
        recycleView.setAdapter(confirmAdapter);
        recycleView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST,
                getResources().getDrawable(R.color.bg_gray)));
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.tvScan://扫描
                nextActivityWithCode(CaptureActivity.class, REQUEST_CODE);
                break;

            case R.id.tvInput:
                llInput.setVisibility(View.VISIBLE);
                break;

            case R.id.tvSub://提交

                if (TextUtils.isEmpty(etNum.getText())) {
                    showErr("请输入需要认证的手机号");
                    return;
                }

                if (etNum.getText().length() != 11) {
                    showErr("请输入正确的手机号码");
                    return;
                }

                showConfirmInfo();
                chageScreen(0.1f);
                break;

            case R.id.tvSubInfo:

                if (etNum.getText().toString().equals(etTel.getText().toString())) {

                    iidConfirmePresenter.checkNum();

                    if (confirmDiag != null && confirmDiag.isShowing())
                        confirmDiag.dismiss();
                } else {
                    showErr("两次输入的手机号码不一致");
                }

                break;
        }

    }

    private String TAG = getClass().getSimpleName();
    private ProgressDialog confirmDiag;
    private EditText etTel;

    public void showConfirmInfo() {

        confirmDiag = new ProgressDialog(this);
        confirmDiag.setCanceledOnTouchOutside(true);
        confirmDiag.setCancelable(true);
        confirmDiag.show();
        confirmDiag.setContentView(R.layout.dialog_confirm_info);
        confirmDiag.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        confirmDiag.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        etTel = (EditText) confirmDiag.findViewById(R.id.etTel);
        confirmDiag.findViewById(R.id.tvSubInfo).setOnClickListener(this);
        confirmDiag.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                chageScreen(1f);
            }
        });
    }

    private void chageScreen(float f) {

        Window window = getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
//        wl.flags=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        wl.alpha = f;   //这句就是设置窗口里崆件的透明度的．０.０全透明．１.０不透明．
        window.setAttributes(wl);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);

                    Log.d(TAG, "解析结果:" + result);
                    if (!TextUtils.isEmpty(result))
                        iidConfirmePresenter.checkImei(result);

                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    showErr("解析二维码失败");
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iidConfirmePresenter.clearRequest();
    }

    @Override
    public String getNum() {
        return etNum.getText().toString();
    }

    @Override
    public void setAdapter(List<ConfirmHistoryEnty> entry) {
        this.entry = entry;
        confirmAdapter.setEnties(entry);
        confirmAdapter.notifyDataSetChanged();
    }
}
